﻿using Sidvall.Serialization;
using System.Linq;

namespace Sidvall.WorkItemManager.Data.ConfigModel.EntityManagers
{
    public sealed partial class ScheduleManager<TEntity, TList> : global::Sidvall.Data.EntityManagerBase
        where TEntity : global::Sidvall.WorkItemManager.Data.ISchedule
        where TList : Sidvall.Data.IDataRowProvider<ISchedule>
    {
        #region Public Members

        #region AcceptChanges

        public void AcceptChanges(global::Sidvall.WorkItemManager.Data.ISchedule value)
        {
            AcceptChanges(value, null);
        }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters")]
        public void AcceptChanges(global::Sidvall.WorkItemManager.Data.ISchedule value, Sidvall.Data.AcceptChangesSettings acceptChangesSettings)
        {
            if (value == null)
                return;
            if (value.DataRowStateIsDirty)
            {
                if (acceptChangesSettings == null)
                    acceptChangesSettings = new Sidvall.Data.AcceptChangesSettings();
                Sidvall.Data.EntityManager.AcceptItemChanges(value, acceptChangesSettings);
            }
        }
        public void AcceptChanges(System.Collections.Generic.IEnumerable<global::Sidvall.WorkItemManager.Data.ISchedule> value)
        {
            AcceptItemsChanges(value, null);
        }
        public void AcceptChanges(System.Collections.Generic.IEnumerable<global::Sidvall.WorkItemManager.Data.ISchedule> value, Sidvall.Data.AcceptChangesSettings acceptChangesSettings)
        {
            AcceptItemsChanges(value, acceptChangesSettings);
        }

        #endregion
        #region SetBusinessSettings

        public void SetBusinessSettings(global::Sidvall.WorkItemManager.Data.ISchedule value, global::Sidvall.Business.BusinessSettings businessSettings, global::Sidvall.Business.BusinessCollectionSettings businessCollectionSettings)
        {
            if (value == null)
                return;
            var provider = value as global::Sidvall.Business.IBusinessSettingsProvider;
            if (provider != null)
                provider.BusinessSettings.Import(businessSettings);
        }
        public void SetBusinessSettings(System.Collections.Generic.IEnumerable<global::Sidvall.WorkItemManager.Data.ISchedule> value, global::Sidvall.Business.BusinessSettings businessSettings, global::Sidvall.Business.BusinessCollectionSettings businessCollectionSettings)
        {
            if (value == null)
                return;
            var provider = value as global::Sidvall.Business.IBusinessCollectionSettingsProvider;
            if (provider != null)
                provider.BusinessSettings.Import(businessCollectionSettings);
            foreach (var item in value)
                SetBusinessSettings(item, businessSettings, businessCollectionSettings);
        }

        #endregion
        #region GetDataRowStateCounts

        public global::Sidvall.Data.DataRowStateCountItemCollection GetDataRowStateCounts(global::Sidvall.WorkItemManager.Data.ISchedule value)
        {
            var items = new global::Sidvall.Data.DataRowStateCountItemCollection();
            LoadDataRowStateCounts(items, value);
            return items;
        }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic")]
        internal void LoadDataRowStateCounts(global::Sidvall.Data.DataRowStateCountItemCollection items, global::Sidvall.WorkItemManager.Data.ISchedule value)
        {
            if (value == null)
                return;
            items.Add(global::Sidvall.WorkItemManager.Data.ConfigModel.DomainObjects.Schedule, value);
        }
        public global::Sidvall.Data.DataRowStateCountItemCollection GetDataRowStateCounts(System.Collections.Generic.IEnumerable<global::Sidvall.WorkItemManager.Data.ISchedule> value)
        {
            var items = new global::Sidvall.Data.DataRowStateCountItemCollection();
            LoadDataRowStateCounts(items, value);
            return items;
        }
        internal void LoadDataRowStateCounts(global::Sidvall.Data.DataRowStateCountItemCollection items, System.Collections.Generic.IEnumerable<global::Sidvall.WorkItemManager.Data.ISchedule> value)
        {
            if (value == null)
                return;
            items.Add(global::Sidvall.WorkItemManager.Data.ConfigModel.DomainObjects.Schedule, value as Sidvall.Data.IListContextProvider);
            foreach (var item in value)
                LoadDataRowStateCounts(items, item);
        }

        #endregion
        #region LoadInstanceField

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
        public void LoadInstanceField(System.Collections.Generic.IEnumerable<global::Sidvall.WorkItemManager.Data.ISchedule> items, string fieldName, object value, bool cascade, bool raisePropertyChanged, bool updateState)
        {
            if (items == null)
                return;
            foreach (var item in items)
                LoadInstanceField(item, fieldName, value, cascade, raisePropertyChanged, updateState);
        }
        partial void OnAfterLoadInstanceField(string fieldName, object value, bool cascade, bool raisePropertyChanged, bool updateState);
        partial void OnBeforeLoadInstanceField(string fieldName, object value, bool cascade, bool raisePropertyChanged, bool updateState);
        partial void OnLoadInstanceField(string fieldName, object value, bool cascade, bool raisePropertyChanged, bool updateState);
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "cascade"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1800:DoNotCastUnnecessarily"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1505:AvoidUnmaintainableCode")]
        public void LoadInstanceField(global::Sidvall.WorkItemManager.Data.ISchedule item, string fieldName, object value, bool cascade, bool raisePropertyChanged, bool updateState)
        {
            if (item == null)
                return;
            if (fieldName == null)
                return;
            OnBeforeLoadInstanceField(fieldName, value, cascade, raisePropertyChanged, updateState);
            switch (fieldName)
            {
                case "DayEndTime":
                    value = Sidvall.Business.ValidationManager.GetValidUniversalDateTime(value, System.DateTimeKind.Utc);
                    item.DayEndTime = (System.Nullable<System.DateTime>)value;
                    Sidvall.Data.EntityManager.OnPropertyChanged(item, "DayEndTime", raisePropertyChanged, updateState);
                    break;
                case "DayFrequency":
                    if (value == null)
                        return;
                    value = Sidvall.Business.ValidationManager.GetValidByte(value, 1, null);
                    item.DayFrequency = (byte)value;
                    Sidvall.Data.EntityManager.OnPropertyChanged(item, "DayFrequency", raisePropertyChanged, updateState);
                    break;
                case "DayFrequencyTypeId":
                    if (value == null)
                        return;
                    item.DayFrequencyTypeId = (global::Sidvall.WorkItemManager.Workflow.Conditions.DayFrequencyType)value;
                    Sidvall.Data.EntityManager.OnPropertyChanged(item, "DayFrequencyTypeId", raisePropertyChanged, updateState);
                    break;
                case "DayStartTime":
                    value = Sidvall.Business.ValidationManager.GetValidUniversalDateTime(value, System.DateTimeKind.Utc);
                    item.DayStartTime = (System.Nullable<System.DateTime>)value;
                    Sidvall.Data.EntityManager.OnPropertyChanged(item, "DayStartTime", raisePropertyChanged, updateState);
                    break;
                case "EndDate":
                    value = Sidvall.Business.ValidationManager.GetValidUniversalDateTime(value, System.DateTimeKind.Utc);
                    item.EndDate = (System.Nullable<System.DateTime>)value;
                    Sidvall.Data.EntityManager.OnPropertyChanged(item, "EndDate", raisePropertyChanged, updateState);
                    break;
                case "Friday":
                    if (value == null)
                        return;
                    item.Friday = (bool)value;
                    Sidvall.Data.EntityManager.OnPropertyChanged(item, "Friday", raisePropertyChanged, updateState);
                    break;
                case "Monday":
                    if (value == null)
                        return;
                    item.Monday = (bool)value;
                    Sidvall.Data.EntityManager.OnPropertyChanged(item, "Monday", raisePropertyChanged, updateState);
                    break;
                case "MonthFrequency":
                    if (value == null)
                        return;
                    value = Sidvall.Business.ValidationManager.GetValidByte(value, 1, null);
                    item.MonthFrequency = (byte)value;
                    Sidvall.Data.EntityManager.OnPropertyChanged(item, "MonthFrequency", raisePropertyChanged, updateState);
                    break;
                case "MonthFrequencyTypeId":
                    if (value == null)
                        return;
                    item.MonthFrequencyTypeId = (global::Sidvall.WorkItemManager.Workflow.Conditions.MonthFrequencyType)value;
                    Sidvall.Data.EntityManager.OnPropertyChanged(item, "MonthFrequencyTypeId", raisePropertyChanged, updateState);
                    break;
                case "Saturday":
                    if (value == null)
                        return;
                    item.Saturday = (bool)value;
                    Sidvall.Data.EntityManager.OnPropertyChanged(item, "Saturday", raisePropertyChanged, updateState);
                    break;
                case "ScheduleFrequency":
                    if (value == null)
                        return;
                    value = Sidvall.Business.ValidationManager.GetValidByte(value, 1, null);
                    item.ScheduleFrequency = (byte)value;
                    Sidvall.Data.EntityManager.OnPropertyChanged(item, "ScheduleFrequency", raisePropertyChanged, updateState);
                    break;
                case "ScheduleFrequencyTypeId":
                    if (value == null)
                        return;
                    item.ScheduleFrequencyTypeId = (global::Sidvall.WorkItemManager.Workflow.Conditions.ScheduleFrequencyType)value;
                    Sidvall.Data.EntityManager.OnPropertyChanged(item, "ScheduleFrequencyTypeId", raisePropertyChanged, updateState);
                    break;
                case "ScheduleId":
                    value = Sidvall.Business.ValidationManager.GetValidInt64(value, null, null);
                    item.ScheduleId = (System.Nullable<long>)value;
                    Sidvall.Data.EntityManager.OnPropertyChanged(item, "ScheduleId", raisePropertyChanged, updateState);
                    break;
                case "StartTime":
                    if (value == null)
                        return;
                    value = Sidvall.Business.ValidationManager.GetValidUniversalDateTime(value, System.DateTimeKind.Utc);
                    item.StartTime = (System.DateTime)value;
                    Sidvall.Data.EntityManager.OnPropertyChanged(item, "StartTime", raisePropertyChanged, updateState);
                    break;
                case "Sunday":
                    if (value == null)
                        return;
                    item.Sunday = (bool)value;
                    Sidvall.Data.EntityManager.OnPropertyChanged(item, "Sunday", raisePropertyChanged, updateState);
                    break;
                case "Thursday":
                    if (value == null)
                        return;
                    item.Thursday = (bool)value;
                    Sidvall.Data.EntityManager.OnPropertyChanged(item, "Thursday", raisePropertyChanged, updateState);
                    break;
                case "Tuesday":
                    if (value == null)
                        return;
                    item.Tuesday = (bool)value;
                    Sidvall.Data.EntityManager.OnPropertyChanged(item, "Tuesday", raisePropertyChanged, updateState);
                    break;
                case "Wednesday":
                    if (value == null)
                        return;
                    item.Wednesday = (bool)value;
                    Sidvall.Data.EntityManager.OnPropertyChanged(item, "Wednesday", raisePropertyChanged, updateState);
                    break;
                default:
                    OnLoadInstanceField(fieldName, value, cascade, raisePropertyChanged, updateState);
                    break;
            }
            OnAfterLoadInstanceField(fieldName, value, cascade, raisePropertyChanged, updateState);
        }

        #endregion
        #region Clone

        public TEntity Clone(ISchedule value)
        {
            return Copy(value, Sidvall.Data.CopySettings.Clone());
        }
        public TList Clone(System.Collections.Generic.IEnumerable<ISchedule> value)
        {
            return Copy(value, Sidvall.Data.CopySettings.Clone());
        }

        #endregion
        #region Copy

        public TEntity Copy(ISchedule value)
        {
            return Copy(value, null, string.Empty, string.Empty);
        }
        public TEntity Copy(ISchedule value, Sidvall.Data.CopySettings copySettings)
        {
            return Copy(value, copySettings, string.Empty, string.Empty);
        }
        internal TEntity Copy(ISchedule value, Sidvall.Data.CopySettings copySettings, string parent, string child)
        {
            if (copySettings == null)
                copySettings = new Sidvall.Data.CopySettings();
            if (!Sidvall.Data.DataRowContextManager.IsValid(value, copySettings.RowFilter))
                return default(TEntity);
            var item = (TEntity)this.EntityFacadeManager.ResourceFactory.CreateItem(global::Sidvall.WorkItemManager.Data.ConfigModel.DomainObjects.Schedule, null);
            if (item == null)
                return default(TEntity);
            Import(item, value, copySettings, parent, child);
            return item;
        }

        public TList Copy(System.Collections.Generic.IEnumerable<ISchedule> value)
        {
            return Copy(value, null, string.Empty, string.Empty);
        }
        public TList Copy(System.Collections.Generic.IEnumerable<ISchedule> value, Sidvall.Data.CopySettings copySettings)
        {
            return Copy(value, copySettings, string.Empty, string.Empty);
        }
        internal TList Copy(System.Collections.Generic.IEnumerable<ISchedule> value, Sidvall.Data.CopySettings copySettings, string parent, string child)
        {
            var items = (TList)this.EntityFacadeManager.ResourceFactory.CreateItems(global::Sidvall.WorkItemManager.Data.ConfigModel.DomainObjects.Schedule);
            Import(items, value, copySettings, parent, child);
            return items;
        }

        #endregion
        #region GetKey

        public string GetKey(ISchedule item)
        {
            if (item == null)
                return null;
            if (item.ScheduleId == null)
                return null;
            return item.ScheduleId.ToString();
        }
        public string GetKey(long scheduleId)
        {
            return scheduleId.ToString(System.Globalization.CultureInfo.InvariantCulture);
        }
        public string GetKey(Sidvall.Data.Filter<global::Sidvall.WorkItemManager.Data.Fields.ScheduleField> item)
        {
            if (item == null)
                return null;
            var part = item.FilterItems.GetFilterItemValue(Sidvall.WorkItemManager.Data.Fields.ScheduleField.ScheduleId, null);
            if (part == null)
                return null;
            var value = part;
            return value;
        }

        #endregion
        #region ParseKey

        public System.Nullable<long> ParseKey(string key)
        {
            return key.ToNullableInt64();
        }

        #endregion
        #region Import

        public void Import(TEntity destination, ISchedule source)
        {
            Import(destination, source, null, string.Empty, string.Empty);
        }
        public void Import(TEntity destination, ISchedule source, Sidvall.Data.CopySettings copySettings)
        {
            Import(destination, source, copySettings, string.Empty, string.Empty);
        }
        partial void OnAfterImport(TEntity destination, ISchedule source, Sidvall.Data.CopySettings copySettings, string parent, string child);
        partial void OnBeforeImport(TEntity destination, ISchedule source, Sidvall.Data.CopySettings copySettings, ref string parent, ref string child);
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "parent"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "child"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1506:AvoidExcessiveClassCoupling"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity")]
        internal void Import(TEntity destination, ISchedule source, Sidvall.Data.CopySettings copySettings, string parent, string child)
        {
            if (destination == null)
                return;
            if (source == null)
                return;
            if (copySettings == null)
                copySettings = new Sidvall.Data.CopySettings();
            OnBeforeImport(destination, source, copySettings, ref parent, ref child);
            var accessLevelProvider = source as Sidvall.Security.IPropertyAccessLevelProvider;
            if ((copySettings.FieldCopyOption & Sidvall.Business.FieldCopyOptions.AutomaticPrimaryKeys) == Sidvall.Business.FieldCopyOptions.AutomaticPrimaryKeys)
            {
                if (copySettings.IncludedProperty(accessLevelProvider, true, false, false, Sidvall.Data.DataType.Int64, "Schedule.ScheduleId", "Schedule"))
                    destination.ScheduleId = source.ScheduleId;
            }
            if ((copySettings.FieldCopyOption & Sidvall.Business.FieldCopyOptions.ManualNonPrimaryKeys) == Sidvall.Business.FieldCopyOptions.ManualNonPrimaryKeys)
            {
                if (copySettings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.DateTime, "Schedule.DayEndTime", "Schedule"))
                    destination.DayEndTime = source.DayEndTime;
                if (copySettings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.Byte, "Schedule.DayFrequency", "Schedule"))
                    destination.DayFrequency = source.DayFrequency;
                if (copySettings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.Int32, "Schedule.DayFrequencyTypeId", "Schedule"))
                    destination.DayFrequencyTypeId = source.DayFrequencyTypeId;
                if (copySettings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.DateTime, "Schedule.DayStartTime", "Schedule"))
                    destination.DayStartTime = source.DayStartTime;
                if (copySettings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.DateTime, "Schedule.EndDate", "Schedule"))
                    destination.EndDate = source.EndDate;
                if (copySettings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.Boolean, "Schedule.Friday", "Schedule"))
                    destination.Friday = source.Friday;
                if (copySettings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.Boolean, "Schedule.Monday", "Schedule"))
                    destination.Monday = source.Monday;
                if (copySettings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.Byte, "Schedule.MonthFrequency", "Schedule"))
                    destination.MonthFrequency = source.MonthFrequency;
                if (copySettings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.Int32, "Schedule.MonthFrequencyTypeId", "Schedule"))
                    destination.MonthFrequencyTypeId = source.MonthFrequencyTypeId;
                if (copySettings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.Boolean, "Schedule.Saturday", "Schedule"))
                    destination.Saturday = source.Saturday;
                if (copySettings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.Byte, "Schedule.ScheduleFrequency", "Schedule"))
                    destination.ScheduleFrequency = source.ScheduleFrequency;
                if (copySettings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.Int32, "Schedule.ScheduleFrequencyTypeId", "Schedule"))
                    destination.ScheduleFrequencyTypeId = source.ScheduleFrequencyTypeId;
                if (copySettings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.DateTime, "Schedule.StartTime", "Schedule"))
                    destination.StartTime = source.StartTime;
                if (copySettings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.Boolean, "Schedule.Sunday", "Schedule"))
                    destination.Sunday = source.Sunday;
                if (copySettings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.Boolean, "Schedule.Thursday", "Schedule"))
                    destination.Thursday = source.Thursday;
                if (copySettings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.Boolean, "Schedule.Tuesday", "Schedule"))
                    destination.Tuesday = source.Tuesday;
                if (copySettings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.Boolean, "Schedule.Wednesday", "Schedule"))
                    destination.Wednesday = source.Wednesday;
            }
            Sidvall.Data.EntityManager.AfterImportItem(destination, source, copySettings);
            OnAfterImport(destination, source, copySettings, parent, child);
        }

        public void Import(System.Collections.Generic.IEnumerable<ISchedule> destination, System.Collections.Generic.IEnumerable<ISchedule> source)
        {
            Import(destination, source, null, string.Empty, string.Empty);
        }
        public void Import(System.Collections.Generic.IEnumerable<ISchedule> destination, System.Collections.Generic.IEnumerable<ISchedule> source, Sidvall.Data.CopySettings copySettings)
        {
            Import(destination, source, copySettings, string.Empty, string.Empty);
        }
        public void Import(System.Collections.Generic.IEnumerable<ISchedule> destination, System.Collections.Generic.IEnumerable<ISchedule> source, long? totalRowCount, Sidvall.Data.CopySettings copySettings)
        {
            Sidvall.Data.ITotalRowCount rowCountProvider;

            if (source == null)
                return;
            Import(destination, source, copySettings, string.Empty, string.Empty);
            if (totalRowCount != null)
            {
                rowCountProvider = destination as Sidvall.Data.ITotalRowCount;
                if (rowCountProvider != null)
                    rowCountProvider.TotalRowCount = totalRowCount.Value;
            }
        }
        internal void Import(Sidvall.Data.IDataRowProvider<ISchedule> destination, System.Collections.Generic.IEnumerable<ISchedule> source, Sidvall.Data.CopySettings copySettings, string parent, string child)
        {
            Import(destination.DataItems, source, copySettings, parent, child);
        }
        partial void OnBeforeImport(System.Collections.Generic.IEnumerable<ISchedule> destination, System.Collections.Generic.IEnumerable<ISchedule> source, Sidvall.Data.CopySettings copySettings, string parent, string child);
        partial void OnAfterImport(System.Collections.Generic.IEnumerable<ISchedule> destination, System.Collections.Generic.IEnumerable<ISchedule> source, Sidvall.Data.CopySettings copySettings, string parent, string child);
        internal void Import(System.Collections.Generic.IEnumerable<ISchedule> destination, System.Collections.Generic.IEnumerable<ISchedule> source, Sidvall.Data.CopySettings copySettings, string parent, string child)
        {
            OnBeforeImport(destination, source, copySettings, parent, child);
            ImportItems(destination, source, copySettings, parent, child);
            OnAfterImport(destination, source, copySettings, parent, child);
        }

        #endregion
        #region DataRowIsValid

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1506:AvoidExcessiveClassCoupling"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity")]
        public bool DataRowIsValid(ISchedule value, Sidvall.Data.BusinessRuleFilter filter)
        {
            if (value == null)
                return true;
            if (filter == null)
                filter = Sidvall.Data.BusinessRuleFilter.CreateFilter();
            var provider = value as Sidvall.Data.IValidParentProvider;
            if ((provider != null) && (!provider.IsSelfValid(filter)))
                return false;
            return true;
        }
        public bool DataRowIsValid(System.Collections.Generic.IEnumerable<ISchedule> value, Sidvall.Data.BusinessRuleFilter filter)
        {
            if (value == null)
                return true;
            foreach (var item in value)
            {
                if (!DataRowIsValid(item, filter))
                    return false;
            }
            return true;
        }

        #endregion
        #region DataRowStateIsDirty

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1506:AvoidExcessiveClassCoupling"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity")]
        public bool DataRowStateIsDirty(ISchedule value)
        {
            if (value == null)
                return false;
            if (value.DataRowStateIsSelfDirty)
                return true;
            return false;
        }
        public bool DataRowStateIsDirty(System.Collections.Generic.IEnumerable<ISchedule> value)
        {
            return IsDirtyItems(value);
        }

        #endregion
        #region ValidateAsync

        public async System.Threading.Tasks.Task<Sidvall.Data.BusinessRuleCollection> ValidateAsync(ISchedule value)
        {
            return await ValidateAsync(value, (Sidvall.Data.ValidationSettings)null).ConfigureAwait(false);
        }
        public async System.Threading.Tasks.Task<Sidvall.Data.BusinessRuleCollection> ValidateAsync(ISchedule value, Sidvall.Data.ValidationSettings settings)
        {
            var context = new Sidvall.Business.ValidationContext(settings);
            await ValidateAsync(value, context).ConfigureAwait(false);
            return context.AllBusinessRules;
        }
        partial void OnBeforeValidate(ISchedule value, Sidvall.Data.BusinessRuleCollection itemBusinessRules, Sidvall.Business.ValidationContext context);
        partial void OnAfterValidate(ISchedule value, Sidvall.Data.BusinessRuleCollection itemBusinessRules, Sidvall.Business.ValidationContext context);
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1506:AvoidExcessiveClassCoupling")]
        public async System.Threading.Tasks.Task ValidateAsync(ISchedule value, Sidvall.Business.ValidationContext context)
        {
            if (value == null)
                return;
            var itemBusinessRules = new Sidvall.Data.BusinessRuleCollection();
            OnBeforeValidate(value, itemBusinessRules, context);
            await context.TaskContext.DoTaskAsync().ConfigureAwait(false);
            OnAfterValidate(value, itemBusinessRules, context);
            await context.TaskContext.DoTaskAsync().ConfigureAwait(false);
            context.AllBusinessRules.AddRange(context.Settings.IncludeNotBrokenRules, itemBusinessRules);
            var businessRulesProvider = value as Sidvall.Data.IBusinessRulesProvider;
            if (businessRulesProvider != null)
                businessRulesProvider.BusinessRules.Merge(context.Settings.IncludeNotBrokenRules, itemBusinessRules);
        }
        public async System.Threading.Tasks.Task<Sidvall.Data.BusinessRuleCollection> ValidateAsync(System.Collections.Generic.IEnumerable<ISchedule> value)
        {
            return await ValidateAsync(value, (Sidvall.Data.ValidationSettings)null).ConfigureAwait(false);
        }
        public async System.Threading.Tasks.Task<Sidvall.Data.BusinessRuleCollection> ValidateAsync(System.Collections.Generic.IEnumerable<ISchedule> value, Sidvall.Data.ValidationSettings settings)
        {
            var context = new Sidvall.Business.ValidationContext(settings);
            await ValidateAsync(value, context).ConfigureAwait(false);
            return context.AllBusinessRules;
        }
        public async System.Threading.Tasks.Task ValidateAsync(System.Collections.Generic.IEnumerable<ISchedule> value, Sidvall.Business.ValidationContext context)
        {
            if (value == null)
                return;
            foreach (var item in value)
                await ValidateAsync(item, context).ConfigureAwait(false);
        }

        #endregion
        #region Initialize

        public void Initialize(ISchedule value)
        {
            Initialize(value, null);
        }
        partial void OnAfterInitialize(ISchedule value);
        partial void OnBeforeInitialize(ISchedule value);
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
        public void Initialize(ISchedule value, Sidvall.Data.InitializeSettings settings)
        {
            if (value == null)
                return;
            if (settings == null)
                settings = new Sidvall.Data.InitializeSettings();
            OnBeforeInitialize(value);
            var accessLevelProvider = value as Sidvall.Security.IPropertyAccessLevelProvider;
            switch (settings.FieldInitializeOption)
            {
                case Sidvall.Data.FieldInitializeOption.Reset:
                    if ((settings.FieldCopyOption & Sidvall.Business.FieldCopyOptions.AutomaticPrimaryKeys) == Sidvall.Business.FieldCopyOptions.AutomaticPrimaryKeys)
                    {
                        if (settings.IncludedProperty(accessLevelProvider, true, false, false, Sidvall.Data.DataType.Int64, "Schedule.ScheduleId", "Schedule"))
                            value.ScheduleId = null;
                    }
                    if ((settings.FieldCopyOption & Sidvall.Business.FieldCopyOptions.ManualNonPrimaryKeys) == Sidvall.Business.FieldCopyOptions.ManualNonPrimaryKeys)
                    {
                        if (settings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.DateTime, "Schedule.DayEndTime", "Schedule"))
                            value.DayEndTime = null;
                        if (settings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.Byte, "Schedule.DayFrequency", "Schedule"))
                            value.DayFrequency = 1;
                        if (settings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.Int32, "Schedule.DayFrequencyTypeId", "Schedule"))
                            value.DayFrequencyTypeId = global::Sidvall.WorkItemManager.Workflow.Conditions.DayFrequencyType.Onetime;
                        if (settings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.DateTime, "Schedule.DayStartTime", "Schedule"))
                            value.DayStartTime = null;
                        if (settings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.DateTime, "Schedule.EndDate", "Schedule"))
                            value.EndDate = null;
                        if (settings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.Boolean, "Schedule.Friday", "Schedule"))
                            value.Friday = false;
                        if (settings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.Boolean, "Schedule.Monday", "Schedule"))
                            value.Monday = false;
                        if (settings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.Byte, "Schedule.MonthFrequency", "Schedule"))
                            value.MonthFrequency = 1;
                        if (settings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.Int32, "Schedule.MonthFrequencyTypeId", "Schedule"))
                            value.MonthFrequencyTypeId = global::Sidvall.WorkItemManager.Workflow.Conditions.MonthFrequencyType.Day;
                        if (settings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.Boolean, "Schedule.Saturday", "Schedule"))
                            value.Saturday = false;
                        if (settings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.Byte, "Schedule.ScheduleFrequency", "Schedule"))
                            value.ScheduleFrequency = 1;
                        if (settings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.Int32, "Schedule.ScheduleFrequencyTypeId", "Schedule"))
                            value.ScheduleFrequencyTypeId = global::Sidvall.WorkItemManager.Workflow.Conditions.ScheduleFrequencyType.Onetime;
                        if (settings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.DateTime, "Schedule.StartTime", "Schedule"))
                            value.StartTime = System.DateTime.UtcNow;
                        if (settings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.Boolean, "Schedule.Sunday", "Schedule"))
                            value.Sunday = false;
                        if (settings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.Boolean, "Schedule.Thursday", "Schedule"))
                            value.Thursday = false;
                        if (settings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.Boolean, "Schedule.Tuesday", "Schedule"))
                            value.Tuesday = false;
                        if (settings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.Boolean, "Schedule.Wednesday", "Schedule"))
                            value.Wednesday = false;
                    }
                    break;
                case Sidvall.Data.FieldInitializeOption.Validate:
                    if ((settings.FieldCopyOption & Sidvall.Business.FieldCopyOptions.AutomaticPrimaryKeys) == Sidvall.Business.FieldCopyOptions.AutomaticPrimaryKeys)
                    {
                        if (settings.IncludedProperty(accessLevelProvider, true, false, false, Sidvall.Data.DataType.Int64, "Schedule.ScheduleId", "Schedule"))
                            value.ScheduleId = Sidvall.Business.ValidationManager.GetValidInt64(value.ScheduleId, null, null);
                    }
                    if ((settings.FieldCopyOption & Sidvall.Business.FieldCopyOptions.ManualNonPrimaryKeys) == Sidvall.Business.FieldCopyOptions.ManualNonPrimaryKeys)
                    {
                        if (settings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.DateTime, "Schedule.DayEndTime", "Schedule"))
                            value.DayEndTime = Sidvall.Business.ValidationManager.GetValidUniversalDateTime(value.DayEndTime, System.DateTimeKind.Utc);
                        if (settings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.Byte, "Schedule.DayFrequency", "Schedule"))
                            value.DayFrequency = Sidvall.Business.ValidationManager.GetValidByte(value.DayFrequency, 1, null);
                        if (settings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.DateTime, "Schedule.DayStartTime", "Schedule"))
                            value.DayStartTime = Sidvall.Business.ValidationManager.GetValidUniversalDateTime(value.DayStartTime, System.DateTimeKind.Utc);
                        if (settings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.DateTime, "Schedule.EndDate", "Schedule"))
                            value.EndDate = Sidvall.Business.ValidationManager.GetValidUniversalDateTime(value.EndDate, System.DateTimeKind.Utc);
                        if (settings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.Byte, "Schedule.MonthFrequency", "Schedule"))
                            value.MonthFrequency = Sidvall.Business.ValidationManager.GetValidByte(value.MonthFrequency, 1, null);
                        if (settings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.Byte, "Schedule.ScheduleFrequency", "Schedule"))
                            value.ScheduleFrequency = Sidvall.Business.ValidationManager.GetValidByte(value.ScheduleFrequency, 1, null);
                        if (settings.IncludedProperty(accessLevelProvider, false, false, false, Sidvall.Data.DataType.DateTime, "Schedule.StartTime", "Schedule"))
                            value.StartTime = Sidvall.Business.ValidationManager.GetValidUniversalDateTime(value.StartTime, System.DateTimeKind.Utc);
                    }
                    break;
                default:
                    break;
            }
            if (settings.ClearBusinessRules)
            {
                var businessRulesProvider = value as Sidvall.Data.IBusinessRulesProvider;
                if (businessRulesProvider != null)
                    businessRulesProvider.BusinessRules.Clear();
            }
            OnAfterInitialize(value);
        }
        public void Initialize(System.Collections.Generic.IEnumerable<ISchedule> value)
        {
            Initialize(value, null);
        }
        public void Initialize(System.Collections.Generic.IEnumerable<ISchedule> value, Sidvall.Data.InitializeSettings settings)
        {
            InitializeItems(value, settings);
        }

        #endregion
        #region EntityManagerBase Members

        protected override Sidvall.Data.IDataRowContextProvider CopyItem(Sidvall.Data.IDataRowContextProvider destination, Sidvall.Data.CopySettings copySettings, string parent, string child)
        {
            return Copy(destination as ISchedule, copySettings, parent, child);
        }
        protected override void ImportItem(Sidvall.Data.IDataRowContextProvider destination, Sidvall.Data.IDataRowContextProvider source, Sidvall.Data.CopySettings copySettings, string parent, string child)
        {
            Import((TEntity)destination, source as ISchedule, copySettings, parent, child);
        }
        protected override void AcceptItemChanges(Sidvall.Data.IDataRowContextProvider item, Sidvall.Data.AcceptChangesSettings acceptChangesSettings)
        {
            AcceptChanges(item as ISchedule, acceptChangesSettings);
        }
        protected override bool IsDirtyItem(Sidvall.Data.IDataRowContextProvider item)
        {
            return DataRowStateIsDirty(item as ISchedule);
        }
        protected override void InitializeItem(Sidvall.Data.IDataRowContextProvider item, Sidvall.Data.InitializeSettings settings)
        {
            Initialize(item as ISchedule, settings);
        }

        #endregion

        #endregion
        #region Private Members

        private global::Sidvall.WorkItemManager.Data.ConfigModel.IEntityFacadeManager EntityFacadeManager { get; set; }

        #endregion
        #region Constructors

        public ScheduleManager(global::Sidvall.WorkItemManager.Data.ConfigModel.IEntityFacadeManager entityFacadeManager)
        {
            this.EntityFacadeManager = entityFacadeManager;
        }

        #endregion
    }
}